/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */ 
package controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import model.Student;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 *
 * @author phill_000
 */
public class WriteXMLFileUniversity {
 
	public static void main(String name, String continent, String country, String city, String postcode, String address, String coordinator) throws SAXException, ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException, XPathExpressionException {
 
            
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("./web/universityXML_1.xml");
 
        XPathFactory xpathfactory = XPathFactory.newInstance();
        XPath xpath = xpathfactory.newXPath();
 
        
        
        XPathExpression expr = xpath.compile("/Universities/Continent/Country");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        nodes.item(0).setNodeValue("hakuna matata");
        for (int i = 0; i < nodes.getLength(); i++) {
            nodes.item(i).setNodeValue("hakuna matata");
            System.out.println(nodes.item(i).getNodeValue());
        }
    
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("./web/universityXML_1.xml");
        
        /* This part should try to find the correct postion for the root element, not working 100% correctly
        Element root = document.getDocumentElement(); //root = Universities !
        //Aufgabe: root muss richtig zugewiesen werden, damit am ende appendchild(root)
        //an der richtigen stelle eingefuegt wird
        String conID = null;
           switch(continent){
               case "Asia": conID = "01";
                            break;
               case "Australia": conID = "02";
                            break;
               case "Europe": conID="03";
                            break;
           }
        String couID = null;   
           switch(country){
               case "South Korea": couID = "0101";
                            break;
               case "India": couID = "0102";
                            break;
               case "Israel": couID = "0103";
                            break;
               case "Australia": couID ="0201";
                            break;
               case "Sweden": couID ="0301";
                            break;
           }
           
        String citID = null;
            switch(city){
               case "Seoul": couID = "010101";
                            break;
               case "Kochi": couID = "010102";
                            break;
               case "Tel Aviv": couID = "010103";
                            break;
               case "Haifa": couID ="010104";
                            break;
               case "Melbourne": couID ="020101";
                            break;
               case "Boras": couID = "030101";
                            break;
               case "Jonkoping": couID = "030102";
                            break;
            }
            
        if(conID != null){
            if(couID != null){
                if(citID != null){//all exist, add university at this postion
                    
                    root = document.getElementById(citID);
                }
                else{//citID = null => add city
                    root = document.getElementById(couID);
                }
            }
            else{//couID = null => add country
                root = document.getElementById(conID);
            }
        }
        else{ //conID = null => add continent
            System.out.println("rirarutsch");
            //root = document.getDocumentElement();
            root = document.getElementById("a03");
        }
        
        
        //eintragungen vornehmen
        Element university = document.createElement("University");
            root.appendChild(university);

            Element uniname = document.createElement("Name");
            uniname.appendChild(document.createTextNode(name));
            university.appendChild(uniname);
            
            Element unicoordinator = document.createElement("Coordinator");
            unicoordinator.appendChild(document.createTextNode(coordinator));
            university.appendChild(unicoordinator);
            
            Element unipostcode = document.createElement("Postcode");
            unipostcode.appendChild(document.createTextNode(postcode));
            university.appendChild(unipostcode);
            
            Element uniaddress = document.createElement("Address");
            uniaddress.appendChild(document.createTextNode(address));
            university.appendChild(uniaddress);
            
            root.appendChild(university);
            
                   
        //}

        DOMSource source = new DOMSource(document);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        //StreamResult result = new StreamResult("./web/universityXML_1.xml");
        //transformer.transform(source, result);
        System.out.println("File4 saved!");
                */
    }
}